#!/bin/bash

# Input parameters zL, cluster mass (units of 1e14 solar masses) and concentration
# Example:
# 
# nfw_kappamap 0.3 10 5

# clean previous environment (*.log are not removed)

./clean.sh

zl=$1
zs=1.0
mass=$2
c=$3

# Set ksmod path here if needed
ksmod="ksmod"

# OK, in python next time...

kappas=$($ksmod $zl $zs $mass $c 0.25 0.75 | cut -f1 | cut -d' ' -f2)
tmp=$($ksmod $zl $zs $mass $c 0.25 0.75 | cut -f2 | cut -d' ' -f2)
xsL=$(echo "$tmp * 60.0" | bc -l)

eL=0.0
thetaL=0.0

# I set gridhi1 manually as some fraction of xsL radius

nfwgridhi=$(echo "$xsL/2.0" | bc -l)

echo "
set plotmode=2
set gridhi1=$nfwgridhi

startup 1 1
nfw $kappas 0 0 $eL $thetaL 0 0 $xsL 0 0 
0 0 0 0 0 0 0 0 0 0

plotcrit cc.dat
plotgrid grid.dat

# check the size of the tg critical curve
#setsource 1
#  sersic 1.0 0.0 0.0 0.0 0.0 0.03 0 0.5 macro
#  0 0 0 0 0 0 0 0

#SBmap2 -$nfwgridhi $nfwgridhi 512 -$nfwgridhi $nfwgridhi 512 1 ering.fits 3

makekapmap kapmap.txt 1 kapmap.range -$nfwgridhi $nfwgridhi 512 -$nfwgridhi $nfwgridhi 512 0 5


" > nfw_kappamap.gl

gravlens nfw_kappamap.gl

gridhi=$(echo "$xsL/2.0" | bc -l)

echo "
set plotmode=2
set gridhi1=$gridhi

kap2lens kapmap.txt 1 kapmap.range kapmap.lens

loadkapmap kapmap.lens 1 1

startup 1 1
    kapmap 1.0 0 0 0 0 0 0 2 2 1
    0 0 0 0 0 0 0 0 0 0

plotcrit cc_kapmap.dat
plotgrid grid_kapmap.dat

#plotkappa plotkapmap.fits 3 -$gridhi $gridhi 512 -$gridhi $gridhi 512

# check the size of the tg critical curve
#setsource 1
#  sersic 1.0 0.0 0.0 0.0 0.0 0.03 0 0.5 macro
#  0 0 0 0 0 0 0 0
#SBmap2 -$gridhi $gridhi 512 -$gridhi $gridhi 512 1 ering_kapmap.fits 3

# check this kappamap against the kappamap created from the analytic model
#makekapmap kapmap_from_kpmap.txt 1 kapmap_from_kapmap.range -$gridhi $gridhi 512 -$gridhi $gridhi 512

" > load_kappamap.gl

gravlens load_kappamap.gl

echo "kappas=$kappas"
echo "xsL=$xsL"
echo "nfwgridhi=$nfwgridhi"
echo "gridhi=$gridhi"

./plot.sh kapmap_$nfwgridhi.png


